<?php

namespace app\user\controller\mall\goods;

use app\user\controller\mall\Base;

/**
 * 商品评价
 *
 * @icon fa fa-circle-o
 */
class Myappraise extends Base
{
    
    /**
     * Goodsappraises模型对象
     * @var \app\common\model\mall\goods\Appraise
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\goods\Appraise;
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //将按钮中的状态参数加入记录中
                    $params['status'] = input('status',0);

                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace("\\model\\", "\\validate\\", get_class($this->model)));
                        $validate = $this->modelSceneValidate ? $name . '.add' : $name;
                        $result = $this->validate($params,$validate);
                        if(true !== $result){
                            // 验证失败 输出错误信息
                            $this->error($result);
                        }
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->alias("a")
            ->join("__MALL_ORDER_GOODS__ g","a.order_id=g.order_id and a.ordergoods_id=g.id and a.goods_id=g.goods_id and a.goodsspec_id=g.salespec_id and user_id=".$this->auth->id,"LEFT")
            ->join('__MALL_ORDER_ORDER__ o','g.order_id = o.id and a.shop_id=o.shop_id','LEFT')
            ->join("__MALL_GOODS_GOODS__ gg","a.goods_id=gg.id","LEFT")
            ->field('a.*,g.order_id,g.goods_id,g.goodsPrice,g.goodsSpecNames,gg.goodsname,gg.goodsimg,g.salespec_id,g.goodsCode,o.orderNo')
            ->where("a.id",$ids)->find();
        if (!$row) $this->error(__('No Results were found'));
        //$row['goodsSpecNames'] = str_replace('@@_@@', ';', $row['goodsSpecNames']);
        $user_ids = $this->getDataLimitUserIds();
        if (is_array($user_ids)){
            if (!in_array($row[$this->dataLimitField], $user_ids)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()){
            $params = $this->request->post("row/a");
            if ($params){
                try{
                    $goodsScore = (int)$params['goodsraty'];
                    $timeScore = (int)$params['timeraty'];
                    $serviceScore = (int)$params['serviceraty'];
                    //根据评分值，设置状态值
                    $totalScore = $goodsScore + $timeScore + $serviceScore;
                    // 计算好、差评数
                    if($totalScore >= 15*0.9){
                        $params['status'] = "best";
                    }
                    elseif($totalScore < 15*0.6){
                        $params['status'] = "bad";
                    }
                    else{
                        $params['status'] = "good";
                    }

                    //是否采用模型验证
                    if ($this->modelValidate){
                        $name = basename(str_replace("\\model\\", "\\validate\\", get_class($this->model)));
                        $validate = $this->modelSceneValidate ? $name . '.edit' : $name;
                        $result = $this->validate($params,$validate);
                        if(true !== $result){
                            // 验证失败 输出错误信息
                            $this->error($result);
                        }
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false){

                        $this->success();
                    }
                    else{
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e){
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


}
